#   Copyright (C) 2008      Tri Tech Information Systems Inc.
# 
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
# 
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
# 
#   You should have received a copy of the GNU General Public License along
#   with this program; if not, write to the Free Software Foundation, Inc.,
#   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#   
from pyxx.parser import CodeModel
from formatters import generator, formatter, argument_list
from wrapper import Wrapper

class ElementWrapper( Wrapper ):
    """
    Wraps an element of either a namespace or a class.
    I.e. a function or a variable
    """

    # This primary template is used to declare the item
    TEMPLATE = "%(def_type)s(%(defSpec)s)"

    # This template is used in the subclasses used for overloading
    TEMPLATE_OVERLOAD = ""

class MemberVariableWrapper( ElementWrapper ):
    """
    Provides the wrapper for a member variable, automatically determines
    whether to provide readonly or readwrite access
    """
    
    @generator
    @formatter(argument_list)
    def generate_defSpec(self):
        parent_name = self.parent.name
        if self.accessPolicy == CodeModel.AccessPolicy.Protected:
            parent_name = "Wrap_%s" % parent_name
        if self.item.type.indirections and self.item.isStatic:
            return ["%(quotedName)s", parent_name + "::%(name)s"] 
        else:
            return ["%(quotedName)s", "&" + parent_name + "::%(name)s"] 

    def generate_def_type(self):
        if self.item.type.isConstant or self.item.type.indirections:
            return 'def_readonly'
        else:
            return 'def_readwrite'



